package org.fastsyncer.manager.util;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

public class FileUtil {

	public static boolean save(String filename, String data, boolean append) {
		try {
			// 保存文件内容
			FileWriter writer = new FileWriter(filename, append);
			writer.write(data);
			writer.close();
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	/**
	 * 递归删除目录下的所有文件及子目录下所有文件
	 * 
	 * @param dir
	 *            将要删除的文件目录
	 * @return boolean 如果返回true说明目录已被成功删除，如果false,这个方法还会尝试继续删除。
	 */
	public static boolean deleteDir(File dir) {
		if (dir.isDirectory()) {
			String[] children = dir.list();
			// 递归删除目录中的子目录下
			for (int i = 0; i < children.length; i++) {
				boolean success = deleteDir(new File(dir, children[i]));
				if (!success) {
					return false;
				}
			}
		}
		// 目录此时为空，可以删除
		return dir.delete();
	}

	public static StringBuffer read(String filename, int start, int size) {
		try {
			BufferedReader reader = new BufferedReader(new FileReader(filename));
			StringBuffer r = new StringBuffer();
			String line;
			int i = 0;
			int end = start + size;
			while ((line = reader.readLine()) != null) {
				if (start <= i && i < end) {
					r.append(line);
				}
				if (i >= end) {
					break;
				}
				i++;
			}
			reader.close();
			return r;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static int count(String filename) throws IOException {
		if (!new File(filename).exists()) {
			return 0;
		}
		InputStream is = new BufferedInputStream(new FileInputStream(filename));
		byte[] c = new byte[1024];
		int count = 0;
		int readChars = 0;
		while ((readChars = is.read(c)) != -1) {
			for (int i = 0; i < readChars; ++i) {
				if (c[i] == '\n')
					++count;
			}
		}
		is.close();
		return count;
	}

	public static boolean existFile(String fileName) {
		try {
			return new File(fileName).exists();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public static void createFileIfNotExist(String fileName) {
		try {
			File file = new File(fileName);
			if (!file.exists()) {
				file.createNewFile();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void createDirIfNotExist(String fileDir) {
		// 检查文件夹是否创建
		File file = new File(fileDir);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	public static void main(String[] args) throws Exception {

		// String fileDir = "D:/fastsyncer/sourceAdmin_targetXddb";
		String filename = "D:/fastsyncer/sourceAdmin_targetXddb";

		// save(fileDir,filename, new JSONObject(task).toString(), false);
//		System.out.println("ok...");
		read(filename, 0, 3);
		// System.out.println(count(filename));
		// deleteDir(new File(filename));
	}

}